<?php
/**
 * Created by PhpStorm.
 * User: lee
 * Date: 12/9/18
 * Time: 9:22 PM
 */

require_once __DIR__.'/vendor/autoload.php';

use Workerman\Worker;
use Workerman\Connection\AsyncTcpConnection;

$client = new Worker();

$client->onWorkerStart = function () {
    $ws_connection = new AsyncTcpConnection("ws://0.0.0.0:7777");
    // 连上后发送hello字符串
    $ws_connection->onConnect = function($connection){
        $connection->send('hello');
    };
    // 远程websocket服务器发来消息时
    $ws_connection->onMessage = function($connection, $data){
        echo "recv: $data\n";
    };
    // 连接上发生错误时，一般是连接远程websocket服务器失败错误
    $ws_connection->onError = function($connection, $code, $msg){
        echo "error: $msg\n";
    };
    // 当连接远程websocket服务器的连接断开时
    $ws_connection->onClose = function($connection){
        echo "connection closed\n";
    };
    // 设置好以上各种回调后，执行连接操作
    $ws_connection->connect();
};

Worker::runAll();